<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2021/3/10
 * Time: 13:36
 */

namespace App\Http\Controllers\Mini\Circle;


use App\Http\Controllers\Controller;
use App\Models\FleaOrderComment;
use App\Models\Mini\FleaGoodsModel;
use App\Models\User;
use Illuminate\Http\Request;

class FleaGoodsController extends Controller
{
    private $user_id;

    public function __construct()
    {
        $id = auth('user')->id();
        $this->user_id = $id;
    }

    /**
     *
     * 邻里圈商家的发布的商品列表
     * @param Request $request
     *
     */

    public function getLssueGoods(Request $request)
    {
        $user_id = $request->get('user_id');
        if (is_null($user_id)) {
            return $this->error('', '请求失败');
        }

        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $offset = ($page == 1) ? 0 : ($page - 1) * $limit;

        $data = FleaGoodsModel::query()
            ->where('user_id', $user_id)
            ->select('id', 'goods_name', 'goods_content', 'goods_images', 'goods_price', 'goods_status', 'is_sale')
            ->offset($offset)
            ->limit($limit)
            ->get();

        $response['data'] = $data;
        $response['count'] = FleaGoodsModel::query()->where('user_id', $user_id)->count();
        $response['pages'] = ceil($response['count'] / $limit);

        return $this->success($response, '请求成功');
    }

    /**
     *
     * 获取用户简介
     *
     */
    public function getUserIntro(Request $request)
    {
//
        $user_id = $this->user_id;

        if (!$user_id) {
            return $this->error('请先登录');
        }

        $data = User::query()
            ->select("user_intro")
            ->where('id', $user_id)
            ->first();
        $user_intro['user_intro'] = $data['user_intro'];
        return $this->success($user_intro, '请求成功');

    }

    /**
     *
     * 修改用户简介
     *
     */
    public function upUserIntro(Request $request)
    {
//
        $user_id = $this->user_id;

        if (!$user_id) {
            return $this->error('请先登录');
        }

        $user_intro = $request->post('user_intro');
        $data = User::query()
            ->select("user_intro")
            ->where('id', $user_id)
            ->first();
        $updata = User::query()->where(['id' => $user_id])->update(['user_intro' => $user_intro]);

        if ($updata) {
            return $this->success("", '修改成功');
        } else if ($user_intro == $data['user_intro']) {
            return $this->success("", '保存成功');
        }
        return $this->error('修改失败');

    }

    /**
     *
     * 邻里圈商家的评论列表
     *
     */
    public function getLssueGoodsComment(Request $request)
    {
        $user_id = $this->user_id;
        if (is_null($user_id)) {
            return $this->error('', '请求失败');
        }

        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $offset = ($page == 1) ? 0 : ($page - 1) * $limit;

        $data = FleaOrderComment::query()
            ->with('user:id,username,nickname,avatar')
            ->where('from_user_id', $user_id)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $response['data'] = $data;
        $response['count'] = FleaOrderComment::query()->where('from_user_id', $user_id)->count();
        $response['pages'] = ceil($response['count'] / $limit);

        return $this->success($response, '请求成功');
    }


}
